package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dao.Article;
import com.example.health_life.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/articles")
    public Response<List<Article>> getArticleList(@RequestParam(required = false, defaultValue = "1") Integer status) {
        List<Article> articles = articleService.getArticleListWithAuthor(status);
        return Response.newSuccess(articles);
    }

    @GetMapping("/articles/type")
    public Response<List<Article>> getArticleListByType(Byte type, @RequestParam(required = false, defaultValue = "1") Integer status) {
        List<Article> articles = articleService.getArticleListByTypeWithAuthor(type, status);
        return Response.newSuccess(articles);
    }

    @GetMapping("/articles/{id}")
    public Response<Article> getArticleById(@PathVariable Integer id) {
        Article article = articleService.getArticleByIdWithAuthor(id);
        return Response.newSuccess(article);
    }

    @PostMapping("/articles")
    public Response<Integer> addArticle(@RequestBody Article article) {
        int id = articleService.addArticle(article);
        return Response.newSuccess(id);
    }

    @DeleteMapping("/articles/{id}")
    public Response<Integer> deleteArticle(@PathVariable Integer id) {
        int result = articleService.deleteArticle(id);
        return Response.newSuccess(result);
    }

    @PutMapping("/articles/{id}")
    public Response<Integer> updateArticle(@PathVariable Integer id,
                                           @RequestParam(required = false) String title,
                                           @RequestParam(required = false) String content,
                                           @RequestParam(required = false,defaultValue = "-9") Integer type,
                                           @RequestParam(required = false) String coverImg,
                                           @RequestParam(required = false,defaultValue = "-9") Integer view,
                                           @RequestParam(required = false,defaultValue = "-9") Integer favorite,
                                           @RequestParam(required = false,defaultValue = "-9") Integer status) {
        int result = articleService.updateArticle(id,title,content,type,coverImg,favorite,view,status);
        return Response.newSuccess(result);
    }
}
